package com.lfnyxy.web.org;

import com.lfnyxy.common.LogTool;
import com.lfnyxy.common.dto.ApiResult;
import com.lfnyxy.common.dto.CodeMessage;
import com.lfnyxy.common.dto.PagedDataTable;
import com.lfnyxy.entity.org.AppUserEntity;
import com.lfnyxy.query.org.AppUserQuery;
import com.lfnyxy.service.org.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by.
 */
@Controller
@RequestMapping("/appuser")
@ResponseBody
public class AppUserController {
    @Autowired
    AppUserService appUserService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult<Integer> CreateAppUser(@RequestBody AppUserEntity item) {
        int result = 0;
        if (item != null) {
            try {
                if (item.getId() > 0) {
                    result = appUserService.UpdateItem(item);
                } else {
                    result = appUserService.CreateItem(item);
                }
            } catch (Exception ex) {
                LogTool.WriteError(this.getClass().toString() + "001", ex.getMessage());
                return ApiResult.error(CodeMessage.SAVE_FAILED);
            }
        }
        return ApiResult.success(result);
    }

    @RequestMapping(value = {"/plist"}, method = {RequestMethod.GET})
    @ResponseBody
    public PagedDataTable QueryPageList(@RequestParam(value = "start", required = false) int start, @RequestParam(value = "length", required = false) int length, @RequestParam(value = "matchString", required = false) String matchString) {
        List<AppUserEntity> result = null;
        AppUserQuery query = new AppUserQuery();
        query.setStart(start);
        query.setLength(length);
        query.setMatchString(matchString);
        try {
            result = appUserService.QueryPageList(query);
        } catch (Exception ex) {
            LogTool.WriteError(this.getClass().toString() + "002", ex.getMessage());
        }
        return new PagedDataTable<AppUserEntity>(query.getTotal(), result);
    }

    @RequestMapping(value = {"/item"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<AppUserEntity> QueryItem(@RequestBody AppUserQuery query) {
        if (query != null && query.getId() > 0) {
            try {
                AppUserEntity result = appUserService.QueryItem(query.getId());
                return ApiResult.success(result);
            } catch (Exception ex) {
                LogTool.WriteError(this.getClass().toString() + "003", ex.getMessage());
            }
        }
        return ApiResult.error(CodeMessage.GET_ITEM_FAILED);
    }

    @RequestMapping(value = {"/ditem"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<Integer> DeleteItem(@RequestBody AppUserQuery query) {
        try {
            if (query != null && query.getId() > 0) {
                int cnt = appUserService.DeleteItem(query.getId());
                return ApiResult.success(cnt);
            }
        } catch (Exception ex) {
            LogTool.WriteError(this.getClass().toString() + "004", ex.getMessage());
        }
        return ApiResult.error(CodeMessage.DELETE_ITEM_FAILED);
    }

    @RequestMapping(value = {"/all"}, method = {RequestMethod.POST})
    @ResponseBody
    public ApiResult<List<AppUserEntity>> QueryAll() {
        try {
            return ApiResult.success(appUserService.QueryAll());
        } catch (Exception ex) {
            LogTool.WriteError(this.getClass().toString() + "005", ex.getMessage());
            return ApiResult.error(CodeMessage.QUERY_LIST_FAILED);
        }
    }
}